package com.orange.core.util.function;

import java.util.function.Consumer;

@FunctionalInterface
public interface ThrowingConsumer<T, E extends Exception> {

    static <T> Consumer<T> wrapper(ThrowingConsumer<T, Exception> throwingConsumer) {
        return t -> {
            try {
                throwingConsumer.accept(t);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        };
    }

    static <T, E extends RuntimeException> Consumer<T> wrapper(ThrowingConsumer<T, Exception> throwingConsumer, E ex) {
        return t -> {
            try {
                throwingConsumer.accept(t);
            } catch (Exception e) {
                ex.initCause(e);
                throw ex;
            }
        };
    }

    void accept(T t) throws E;
}
